<template>
  <!-- 安全中心 -->
  <div class="mineSafety-box">
    <Header :headerObj='headerObj'></Header>
    <div class="userinfo">
      <div class="toxiang">
        <img :src="userInfo_tx + '?x-oss-process=image/resize,m_fixed,h_140,w_140'" alt="" />
      </div>
      <div class="name">{{ nikename == null ? un : nikename }}</div>
      <div class="id">ID：{{ this.$store.state.userInfo.un }}</div>
    </div>
    <ul class="mine-info">
      <li @click="seeYinsi">
        <div class="mine-info-l">
          <img src="../../assets/images/yinsibaohu@2x.png" alt="" />
        </div>
        <div class="mine-info-r">
          <div class="mine-info-r-text">隐私政策</div>
          <div class="mine-info-r-icon">
            <Iconfont class="arrow-right" iconClass="youjiantou2" />
          </div>
        </div>
      </li>
      <li @click="logout">
        <div class="mine-info-l">
          <img src="../../assets/images/zhuxiao@2x.png" alt="" />
        </div>
        <div class="mine-info-r">
          <div class="mine-info-r-text">注销账号</div>
          <div class="mine-info-r-icon">
            <Iconfont class="arrow-right" iconClass="youjiantou2" />
          </div>
        </div>
      </li>
    </ul>
    <LogoutNotarize :dialogData="dialogData" :controlShow="controlShow"></LogoutNotarize>
    <privacyText v-if="isPrivacy"></privacyText>
  </div>
</template>
<script>
  import privacyText from '../homeComponents/privacyText.vue'
  import LogoutNotarize from "@/components/LogoutNotarize.vue";
  import { cancelQuery, cancelWithdraw } from "@/util/api.js";
  import fun from "@/util/function";

  export default {
    components: {
      privacyText,
      LogoutNotarize,
    },
    data() {
      return {
        isPrivacy: false,
        nikename: this.$store.state.userInfo.nickname || window.localStorage.getItem('userNickName'),
        un: this.$store.state.userInfo.un,
        userInfo_tx: this.$store.state.userInfo.wxpic || imgTx,
        getPlatform: fun.getPlatform(), //app wx
        // 弹窗
        controlShow: false,
        headerObj: {
          'isShowBack': true,
          'isShowClose': false,
          'isShowMine': false,
          'fromType': 'mine',
          'titleText': '账号与安全',
          'color': '#27344B'
        },
        dialogData: {
          title: "账号已申请注销",
          message: "",
          confirmButtonText: "我再想想",
          cancelButtonText: "恢复账号",
          confirmEvent: async () => {
            try {
              const dataMsg = await cancelWithdraw();
              const { code, data } = dataMsg.data;
              if (code === "000000") {
                if (data) {
                  fun.toastMessage("账号恢复成功！");
                } else {
                  fun.toastMessage("账号已恢复");
                }
              } else {
                fun.toastMessage("网络可能出了点小问题");
              }
            } catch (error) {
              fun.toastMessage("网络可能出了点小问题");
              console.error(error);
            }
          },
        },
      };
    },
    methods: {
      seeYinsi() {
        this.isPrivacy = true;
      },
      // 进入注销页面  查询是否已注销 ? 弹出提示框可撤销 : 进入页面 ;
      async logout() {
        try {
          const dataMsg = await cancelQuery();
          const { code, data } = dataMsg.data;
          if (code === "000000" && data !== null) {
            const excdate = new Date(data.excTime);
            const todate = new Date();
            this.dialogData.message = `账号（ "${this.un
              }" ）已申请注销，仅有${this.formatDuring(
                excdate - todate
              )}可撤销申请，过期将自动注销账号。`;
            this.controlShow = false;
            this.controlShow = true;
          } else {
            this.$router.push(`/${this.$channel}/logout`);
          }
        } catch (error) {
          fun.toastMessage("网络可能出了点小问题");
          console.error(error);
        }
      },
      formatDuring(millisecond) {
        let days = Math.floor(millisecond / (1000 * 60 * 60 * 24));
        let hours = (millisecond % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        let minutes = (millisecond % (1000 * 60 * 60)) / (1000 * 60);
        let seconds = (millisecond % (1000 * 60)) / 1000;
        if (days > 0) {
          return days + "天";
        } else if (hours > 0) {
          return Math.floor(hours) + "小时";
        } else if (minutes > 0) {
          return Math.floor(minutes) + "分钟";
        } else if (seconds > 0) {
          return Math.floor(seconds) + "秒";
        } else {
          return 0 + "秒";
        }
      },
    },
    created() {
      this.$store.dispatch("hideNav");
    },
    mounted() {
      this.$store.dispatch("hideNav");
      if (window.topmargin && this.$channel == 'zxjtqh') {//仅app才会有window.topmargin
        fun.setBarHeight(document.getElementById('history-content-box'), window.topmargin * 2, true) //历史记录列表高度
      }
    },
  };
</script>
<style scoped lang="less">
  .mineSafety-box {
    width: 100%;
    height: 100%;
    overflow-y: scroll;
    position: relative;
    .userinfo {
      display: flex;
      margin-top: 120px;
      align-items: center;
      flex-direction: column;
      padding-top: 140px;
      padding-bottom: 100px;

      .toxiang {
        width: 200px;
        height: 200px;
        border-radius: 999px;
        overflow: hidden;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .name {
        margin-top: 30px;
        font-weight: 600;
      }

      .id {
        margin-top: 20px;
        color: #ccc;
        font-weight: 500;
        font-size: 30px;
      }
    }

    .mine-info {
      padding: 0 40px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      li {
        border-bottom: 1px #ededed solid;
        width: 100%;
        height: 110px;
        display: flex;
        justify-content: space-between;
        align-items: center;

        img {
          width: 50px;
          height: 50px;
        }

        .mine-info-l {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 120px;
          height: 100%;

          img {
            object-fit: contain;
            width: 40px;
            height: 40px;
          }
        }

        .mine-info-r {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;

          .mine-info-r-text {
            font-weight: 600;
          }

          .mine-info-r-icon {
            margin-left: 20px;

            .arrow-right {
              font-size: 30px;
              color: #ccc;
              line-height: 110px;
              font-weight: 600;
            }
          }
        }
      }
    }
  }
</style>